import Vue from 'vue'
// 引入 access_token,refreshToken
import { ACCESS_TOKEN,REFRESH_TOKEN } from '@/store/mutation-types'
//引入login.js中方法
import {login,getInfo,getServiceMenuList,logout,refreshToken,validataToken} from '@/api/login'

import { Message } from 'element-ui'
const user = {
  state: {
    accessToken: '',
	refreshToken:'',
	menuList: undefined,
	elementList: undefined,
	roles: [],
	userCode: undefined,
	userAvatar: '',
	userStatus: undefined,
	userPhone: undefined,
	permissionMenuList: undefined,
	userName:undefined
  },
  mutations: {
    SET_ACCESS_TOKEN: (state, accessToken) => {
		state.accessToken = accessToken
    },
	SET_REFRESH_TOKEN: (state, refreshToken) => {
		state.refreshToken = refreshToken
	},
    SET_MENU_LIST: (state, menuList) => {
		console.log('menuList',menuList)
		state.menuList = menuList
    },
    SET_ELEMENT_LIST: (state, elementList) => {
		state.elementList = elementList
    },
	SET_ROLES: (state, roleList) => {
		state.roles = roleList
	},
	SET_USER_CODE: (state, userCode) => {
		console.log('userCode',userCode)
		state.userCode = userCode
	},
	SET_USER_AVATAR: (state, userAvatar) => {
		state.userAvatar = userAvatar
	},
	SET_USER_STATUS: (state, userStatus) => {
		state.userStatus = userStatus
	},
	SET_USER_PHONE: (state, userPhone) => {
		state.userPhone = userPhone
	},
	SET_PERMISSION_MENU_LIST: (state, permissionMenuList) => {
		state.permissionMenuList = permissionMenuList
	},
	SET_USER_NAME: (state,userName)=>{
		state.userName = userName
	}
	
  },
  actions: {
	//登录操作,如果登录成功把返回的token信息保存在vue-ls中,并同步到vuex中
	Login({commit},validataUserAuth){
		return new Promise((resolve, reject) =>{
			login(validataUserAuth).then(response =>{
				const result = response.data;
				if(result.responseCode !== '200'){
					Message({
						message: result.responseMessage,
						type: 'warning'
					})
					return Promise.reject('error')
				}
				//保存accessToken有效时间为30分钟 2 * 60 * 60 * 1000
				Vue.ls.set(ACCESS_TOKEN, result.responseData.accessToken, 30 * 60 * 1000)
				//保存refreshToken有效时间为30天	
				Vue.ls.set(REFRESH_TOKEN, result.responseData.refreshToken, 30 * 24 * 60 * 60 * 1000)
				//同步到vuex中
				commit('SET_ACCESS_TOKEN', result.responseData.accessToken)
				commit('SET_REFRESH_TOKEN', result.responseData.refreshToken)
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	},
	ValidataToken({commit},token){
		return new Promise((resolve, reject) =>{
			validataToken(token).then(response =>{
				const result = response.data;
				if(result.responseCode !== '200'){
					return Promise.reject('error')
				}
				commit
				resolve(result)
			}).catch(error => {
				reject(error)
			})
		})
	},
	RefreshToken({commit,state}){
		return new Promise((resolve, reject) =>{
			refreshToken(state.userCode).then(response =>{
				const result = response.data;
				if(result.responseCode !== '200'){
					Message({
						message: result.responseMessage,
						type: 'warning'
					})
					return Promise.reject('error')
				}
				//保存accessToken有效时间为30分钟
				Vue.ls.set(ACCESS_TOKEN, result.responseData.accessToken, 30 * 60 * 1000)
				//保存refreshToken有效时间为30天
				Vue.ls.set(REFRESH_TOKEN, result.responseData.refreshToken, 30 * 24 * 60 * 60 * 1000)
				//同步到vuex中
				commit('SET_ACCESS_TOKEN', result.responseData.accessToken)
				commit('SET_REFRESH_TOKEN', result.responseData.refreshToken)
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	},
	//获取用户信息 -> 对应 permission下 store.dispatch('GetUserInfo')
    GetUserInfo({commit,state}) {
		return new Promise((resolve, reject) => {
			//1.获取用户基础信息
			getInfo(state.accessToken).then(response => {
				const result = response.data
				//前端返回的信息同步到vuex中
				const menuList = {};
				for(let i = 0 ; i < result.responseData.menuList.length;i++) {
					menuList[result.responseData.menuList[i].authCode] = true
				}
				const elementList = {};
				for(let i = 0 ; i < result.responseData.elementList.length;i++) {
					elementList[result.responseData.elementList[i].authCode] = true
				}
				commit('SET_MENU_LIST', menuList)
				commit('SET_ELEMENT_LIST', elementList)
				commit('SET_ROLES', result.responseData.roles)
				commit('SET_USER_CODE',result.responseData.userCode)
				commit('SET_USER_AVATAR', result.responseData.userAvatar)
				commit('SET_USER_STATUS',result.responseData.userStatus)
				commit('SET_USER_PHONE',result.responseData.userPhone)
				commit('SET_USER_NAME',result.responseData.userName)
				resolve(response.data)
			}).catch(error => {
				reject(error)
			})
			//2.获取用户所拥有的菜单权限信息(根据角色获取)
			getServiceMenuList(state.accessToken).then(response => {
				//保存用户所拥有的菜单权限信息(根据角色获取)到vuex中
				commit('SET_PERMISSION_MENU_LIST', response.data.responseData)
			})
		})
	},
	//登出操作,如果操作成功清空vuex中基础信息
	Logout({commit,state}){
		return new Promise((resolve, reject) => {
			logout(state.accessToken).then(() => {
				console.log('清空缓存,退出登录')
				//清空vue-ls中所存储的信息
				Vue.ls.clear()
				//清空vuex中 user 所保存的信息,恢复到初始值
				commit('SET_ACCESS_TOKEN','')
				commit('SET_REFRESH_TOKEN','')
				commit('SET_ROLES',[])
				commit('SET_MENU_LIST',undefined)
				commit('SET_ELEMENT_LIST',undefined)
				commit('SET_PERMISSION_MENU_LIST',undefined)
				commit('SET_USER_CODE',undefined)
				commit('SET_USER_PHONE',undefined)
				commit('SET_USER_STATUS',undefined)
				commit('SET_USER_AVATAR', '')
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	},
	//权限校验失败前端退出时调用
	FrontEndLogout({commit}){
		return new Promise(resolve => {
			console.log("刷新清空操作进行跳转")
			//清空vue-ls中所存储的信息
			Vue.ls.clear()
			//清空vuex中 user 所保存的信息,恢复到初始值
			commit('SET_ACCESS_TOKEN','')
			commit('SET_REFRESH_TOKEN','')
			commit('SET_ROLES',[])
			commit('SET_MENU_LIST',undefined)
			commit('SET_ELEMENT_LIST',undefined)
			commit('SET_PERMISSION_MENU_LIST',undefined)
			commit('SET_USER_CODE',undefined)
			commit('SET_USER_PHONE',undefined)
			commit('SET_USER_STATUS',undefined)
			resolve()
		})
	}
  }
}
export default user
